<!DOCTYPE html>
<html>

<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">


    <title>ZDH 烽火台新增</title>
    <meta name="keywords" content="ZDH 烽火台新增">
    <meta name="description" content="ZDH 烽火台新增">

    <link rel="shortcut icon" href="img/favicon.ico">

    <link href="css/plugins/chosen/chosen.css" rel="stylesheet">
    <link href="css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="css/font-awesome.css?v=4.4.0" rel="stylesheet">
    <link href="css/plugins/iCheck/custom.css" rel="stylesheet">
    <link href="css/animate.css" rel="stylesheet">
    <link href="css/style.css?v=4.1.0" rel="stylesheet">
    <link href="css/plugins/bootstrap-table/bootstrap-table.min.css" rel="stylesheet">
    <link rel="stylesheet" data-name="vs/editor/editor.main" href="js/plugins/vs/editor/editor.main.css">
    <link rel="stylesheet" href="js/plugins/vs/editor/zdh.css">

</head>

<body class="gray-bg">
<div class="wrapper wrapper-content animated fadeInRight">

    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>烽火台配置</h5>
                    <div class="ibox-tools">
                        <a class="collapse-link">
                            <i class="fa fa-chevron-up"></i>
                        </a>
                        <a class="dropdown-toggle" data-toggle="dropdown" href="buttons.html#">
                            <i class="fa fa-eye"></i>
                        </a>
                        <ul class="dropdown-menu dropdown-user">
                            <li><a href="javascript:void(0);" onclick="getResourceDesc()">功能说明</a>
                            </li>
                        </ul>
                    </div>
                </div>
                <div class="ibox-content">

                    <form id="etl_task_add_form" name="etl_task_add_form" method="post" class="form-horizontal"
                          action="">

                        <div class="form-group">
                            <label class="col-sm-2 control-label">归属产品</label>
                            <div class="col-sm-10">
                                <select id="product_code" name="product_code"
                                        data-placeholder="归属产品...."
                                        class="chosen-select form-control m-b" >
                                </select>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">归属组</label>
                            <div class="col-sm-10">
                                <select id="dim_group" name="dim_group"
                                        data-placeholder="归属组...."
                                        class="chosen-select form-control m-b" >
                                </select>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">烽火任务说明</label>
                            <div class="col-sm-10">
                                <input id="beacon_fire_context" name="beacon_fire_context" type="text"
                                       placeholder="烽火任务说明" class="form-control" aria-required="true"> </span>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">创建时间</label>
                            <div class="col-sm-10">
                                <input id="create_time" name="create_time" type="text" readonly
                                       placeholder="创建时间" class="form-control" aria-required="true"> </span>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">表达式</label>
                            <div class="col-sm-10">
                                <input id="expr" name="expr" type="text"
                                       placeholder="example:100s,100m,100h,1d or corn 表达式,默认1d" class="form-control" aria-required="true"> </span>
                                <span class="help-block m-b-none"><a id="" href="javascript:void(0);" onclick="cron()">生成Cron表达式</a></span>
                            </div>
                        </div>

                        <div class="form-group" id="div_time_diff">
                            <label class="col-sm-2 control-label">回退时间差</label>
                            <div class="col-sm-10">
                                <input id="time_diff" name="time_diff" type="text"
                                       placeholder="时间差 example:1s(1秒),1m(1分钟),1d(1天)" class="form-control" aria-required="true"> </span>
                            </div>
                        </div>

                        <div class="hr-line-dashed"></div>
                        <!-- 输入数据源开始-->
                        <div class="form-group">
                            <label class="col-sm-2 control-label">输入数据源选择</label>
                            <div class="col-sm-10">
                                <select id="data_sources_choose_input" name="data_sources_choose_input"
                                        data-placeholder="选择数据源..."
                                        class="chosen-select form-control m-b" tabindex="2">
                                    <option value="" mytype="">选择数据源...</option>
                                </select>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">数据源类型</label>
                            <div class="col-sm-10">
                                <input id="data_source_type_input" name="data_source_type_input" type="text" readonly
                                       placeholder="数据源类型" class="form-control" aria-required="true"> </span>
                            </div>
                        </div>


                        <div class="form-group">
                            <label class="col-sm-2 control-label">sql</label>
                            <div class="col-sm-10" id="div_data_sources_params">
                                <div id="sql_script" name="sql_script" class="monaco_div_100"></div>
                                <span class="help-block m-b-none"></span>
                            </div>

                        </div>

                        <div class="hr-line-dashed"></div>

                        <!-- 输入数据源结束-->

                        <!-- 输出数据源开始-->

                        <div class="form-group">
                            <label class="col-sm-2 control-label">处理脚本</label>
                            <div class="col-sm-10" id="div_data_sources_params_output">
                                <div id="groovy_script" name="groovy_script" class="monaco_div_100"></div>
                                <span class="help-block m-b-none"><a id="a_params" href="javascript:void(0);"
                                                                     onclick="code_demo()">脚本例子</a></span>
                            </div>
                        </div>


                        <div class="hr-line-dashed"></div>
                        <!-- 元信息 业务相关-->


                        <div class="form-group">
                            <label class="col-sm-2 control-label">告警级别</label>
                            <div class="col-sm-10">
                                <select id="alarm_level" name="alarm_level"
                                        data-placeholder="告警级别...."
                                        class="chosen-select form-control m-b" >
                                    <option value="P0" mytype="">P0</option>
                                    <option value="P1" mytype="">P1</option>
                                    <option value="P2" mytype="">P2</option>
                                    <option value="P3" mytype="">P3</option>
                                </select>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">告警组</label>
                            <div class="col-sm-10">
                                <select id="alarm_group" name="alarm_group"
                                        data-placeholder="告警组...."
                                        class="chosen-select form-control m-b" >
                                </select>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">告警频次</label>
                            <div class="col-sm-10">
                                <input id="frequency_config" name="frequency_config" type="text"
                                       placeholder="频次 example:20,1 表示20秒内最多告警1次" class="form-control" aria-required="true"> </span>
                            </div>
                        </div>

                        <div class="hr-line-dashed"></div>
                        <!-- 输出数据源结束-->

                        <div class="form-group">
                            <div class="text-center">
                                <button id="save_etl_task" name="save_zdh" class="btn btn-primary"
                                        οnsubmit='return false'
                                        type="button">保存
                                </button>
                                <button id="update_etl_task" name="save_zdh" class="btn btn-primary"
                                        οnsubmit='return false'
                                        type="button">更新
                                </button>
                                <button id="reset" class="btn btn-white" type=reset>清空</button>
                            </div>
                        </div>


                    </form>


                </div>
            </div>


        </div>
    </div>
</div>


<script src="js/zdh_common.js"></script>
<script>
    var require = {
        paths: {
            'vs': 'js/plugins/vs'
        }
    };

</script>
<!-- 全局js -->
<script src="js/jquery.min.js?v=2.1.4"></script>
<script src="js/bootstrap.min.js?v=3.3.6"></script>

<!-- Bootstrap table -->
<script src="js/plugins/bootstrap-table/bootstrap-table.min.js"></script>
<script src="js/plugins/bootstrap-table/locale/bootstrap-table-zh-CN.min.js"></script>
<!-- 自定义js -->
<script src="js/content.js?v=1.0.0"></script>

<!-- Chosen -->
<script src="js/plugins/chosen/chosen.jquery.js"></script>

<!-- layer javascript -->
<script src="js/plugins/layer/layer.min.js"></script>

<script src="js/plugins/vs/loader.js"></script>
<script src="js/plugins/vs/editor/editor.main.nls.js"></script>
<script src="js/plugins/vs/editor/editor.main.js"></script>
<script type="text/javascript" src="js/plugins/vs/basic-languages/sql/sql.js"></script>
<script type="text/javascript" src="js/plugins/vs/basic-languages/java/java.js"></script>

<script src="js/admin/dim_product_common.js"></script>
<script src="js/admin/dim_group_common.js"></script>

<!-- iCheck -->
<script src="js/plugins/iCheck/icheck.min.js"></script>
<script>
    $(document).ready(function () {
        $('.i-checks').iCheck({
            checkboxClass: 'icheckbox_square-green',
            radioClass: 'iradio_square-green',
        });
    });

    (function (document, window, $) {


        (function () {

            var url = location.search; //这一条语句获取了包括问号开始到参数的最后，不包括前面的路径
            var params = url.substr(1);//去掉问号
            var pa = params.split("&");
            var s = new Object();//定义一个全局变量-存储任务ID
            for (var i = 0; i < pa.length; i++) {
                s[pa[i].split("=")[0]] = unescape(pa[i].split("=")[1]);
            }

            window.editor_sql_script = monaco.editor.create(document.getElementById('sql_script'), {
                value: '-- 清空条件,使用sql语法 example: delete from user where etl_date=2019-12-01,如果是hbase数据源直接输入start_row_key,end_row_key 通过逗号分割 example: 001,100 ,提交时此语句此提示语必须删除',
                language: 'sql',
                theme:'vs-dark'
            });

            window.editor_groovy_script = monaco.editor.create(document.getElementById('groovy_script'), {
                value: 'groovy 语法',
                language: 'java',
                theme:'vs-dark'
            });

            $('#alarm_level').chosen();

            function init_alarm_code(){
                $.ajax({
                    type: 'POST',
                    url: server_context+"/beacon_fire_alarm_group_list_by_owner",
                    async:false,
                    dataType: 'json',
                    data: "",
                    //成功返回
                    success: function (data) {
                        if(data.code != "200"){
                            layer.msg(data.msg);
                            return ;
                        }
                        var str = '<option value=\"'  + '\" hassubinfo=\"true\">' + '选择归属组' + '</option>';
                        for (var i = 0; i < data.result.length; i++) {
                            str += '<option value=\"' + data.result[i].alarm_group_code + '\" hassubinfo=\"true\">' + data.result[i].alarm_group_context + '</option>';
                        }
                        console.info(data.result.length);
                        $('#alarm_group').html(str);
                        $("#alarm_group").trigger("chosen:updated");
                        $('#alarm_group').chosen();
                    },
                    //处理完成
                    complete: function () {
                    },
                    //报错
                    error: function (data) {
                    }
                });
            }

            //定义特定数据源展示的div
            function fileShow() {
                console.info("fileShow");
            }

            //初始化数据源--把所有数据源都加载到下拉框
            function init_data_sources() {
                $.ajax({
                    type: 'POST',
                    url: server_context+"/data_sources_list2",
                    dataType: 'json',
                    async: false,
                    data: {"data_source_type": "JDBC"},
                    //成功返回
                    success: function (data) {
                        if(data.code != "200"){
                            layer.msg(data.msg);
                            return ;
                        }
                        let str = '<option value=\"\" mytype=\"\" hassubinfo=\"true\">空</option>';
                        for (let i = 0; i < data.result.length; i++) {
                            str += '<option value=\"' + data.result[i].id + "\" mytype=\"" + data.result[i].data_source_type + "\" hassubinfo=\"true\">" + data.result[i].data_source_context + "</option>"
                        }
                        //输入数据源类型下拉框
                        $('#data_sources_choose_input').html(str);
                        $('#data_sources_choose_input').chosen().on("change", function (evt, params) {
                            // alert($('#data_sources_choose_input').val())
                            $('#data_source_type_input').val($('#data_sources_choose_input').find("option:selected").attr("mytype"));
                        });
                    },
                    //处理完成
                    complete: function () {
                        console.info("complete")
                    },
                    //报错
                    error: function (data) {
                        console.info("error: " + data.responseText);
                    }
                });
            }

            fileShow();
            //启动数据源select 初始化函数
            init_data_sources();

            init_alarm_code();
            //s.id=-1 表示新增,否则表示更新
            if (s.id == -1) {
                console.info("新增按钮触发");
                //$('#data_sources_table_name_input').chosen();
                //$('#data_sources_table_name_output').chosen();
                $('#update_etl_task').hide();
                $('#save_etl_task').show();
            } else {

                //init_table_name(s.id)

                if (s.is_copy == "true") {
                    console.info("拷贝按钮触发");
                    $('#update_etl_task').hide();
                    $('#save_etl_task').show();
                } else {
                    console.info("更新按钮触发");
                    $('#save_etl_task').hide();
                    $('#update_etl_task').show();
                }

                if (s.only_read == "true") {
                    $('#update_etl_task').hide();
                    $('#save_etl_task').hide();
                    $('#reset').hide();
                }
                buildParam()
            }

            function getMyDate(str) {
                var oDate = new Date(str),
                    oYear = oDate.getFullYear(),
                    oMonth = oDate.getMonth() + 1,
                    oDay = oDate.getDate(),
                    oHour = oDate.getHours(),
                    oMin = oDate.getMinutes(),
                    oSen = oDate.getSeconds(),
                    oTime = oYear + '-' + getzf(oMonth) + '-' + getzf(oDay) + " " + getzf(oHour) + ":" + getzf(oMin) + ":" + getzf(oSen);//最后拼接时间
                return oTime;
            };

            //补0操作
            function getzf(num) {
                if (parseInt(num) < 10) {
                    num = '0' + num;
                }
                return num;
            }


            function buildParam() {
                if (s.id != '-1') {
                    console.info("更新按钮触发--开始赋值对应参数");
                    //开始赋值

                    $.ajax({
                        url: server_context+"/beacon_fire_detail",
                        data: "id=" + s.id,
                        type: "post",
                        async: false,
                        dataType: "json",
                        success: function (data) {
                            console.info("success");
                            // layer.alert(JSON.stringify(data[0]))

                            var product_code=data.result.product_code;
                            $("#product_code").val(product_code);
                            $("#product_code").trigger("chosen:updated");

                            var dim_group=data.result.dim_group;
                            $("#dim_group").val(dim_group);
                            $("#dim_group").trigger("chosen:updated");

                            //获取ETL 说明
                            var beacon_fire_context = data.result.beacon_fire_context;
                            $("#beacon_fire_context").val(beacon_fire_context);

                            var create_time = data.result.create_time;
                            $("#create_time").val(getMyDate(create_time));

                            var expr = data.result.expr;
                            $("#expr").val(expr);

                            var time_diff = data.result.time_diff;
                            $("#time_diff").val(time_diff);

                            //获取输入数据源id
                            var data_sources_choose_input = data.result.data_sources_choose_input;
                            console.info("参数赋值:输入数据源id:" + data_sources_choose_input);
                            //初始化输入数据源select 组件
                            $("#data_sources_choose_input").val(data_sources_choose_input);
                            $("#data_sources_choose_input").trigger("chosen:updated");

                            //获取输入数据源类型
                            var data_source_type_input = data.result.data_source_type_input;
                            $("#data_source_type_input").val(data_source_type_input);

                            //输出文件类型参数
                            var alarm_level = data.result.alarm_level;
                            $("#alarm_level").val(alarm_level);
                            $("#alarm_level").trigger("chosen:updated");


                            var sql_script = data.result.sql_script;
                            //$("#data_sources_params_input").val(data_sources_params_input);
                            window.editor_sql_script.setValue(sql_script);


                            var groovy_script = data.result.groovy_script;
                            //$('#data_sources_filter_input').val(data_sources_filter_input);
                            window.editor_groovy_script.setValue(groovy_script);

                            var alarm_group = data.result.alarm_group;
                            $("#alarm_group").val(alarm_group);
                            $("#alarm_group").trigger("chosen:updated");

                            var frequency_config = data.result.frequency_config;
                            $("#frequency_config").val(frequency_config);

                            parent.layer.msg(data.msg);
                        },
                        //处理完成
                        complete: function (data) {
                        },
                        error: function (data) {
                            console.info("error: " + data.responseText);
                        }

                    });


                }
            }

        })();

    })(document, window, jQuery);

    function cron() {
        parent.layer.open({
            type: 2,
            title: 'cron表达式生成',
            shadeClose: false,
            resize: true,
            fixed: false,
            maxmin: true,
            shade: 0.1,
            area : ['45%', '60%'],
            //area: ['450px', '500px'],
            content: server_context+"/cron.html", //iframe的url
            end : function () {
                console.info("弹框结束")
            }
        });
    }

    function code_demo() {
        window.open(server_context+'/beacon_fire_demo_index?id=-1');
    }

    $('#save_etl_task').click(function () {

        if ($('#beacon_fire_context').val() == '') {
            layer.msg("烽火台说明不能为空！");
            return;
        }

        var editor_str = "&sql_script="+window.editor_sql_script.getValue();
        editor_str = editor_str +"&groovy_script="+window.editor_groovy_script.getValue();

        $.ajax({
            type: 'POST',
            url: server_context+"/beacon_fire_add",
            async: false,
            dataType: 'json',
            data: $("#etl_task_add_form").serialize() +editor_str,
            //发送数据前
            beforeSend: function () {
                // 禁用按钮防止重复提交
                $("#save_etl_task").attr({disabled: "disabled"});
            },
            //成功返回
            success: function (data) {
                $("#save_etl_task").removeAttr('disabled');
                if(data.code != '200'){
                    console.error(data.msg);
                    layer.msg("执行失败");
                    return ;
                }
                layer.msg("执行成功");
                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                parent.layer.close(index); //再执行关闭
            },
            //处理完成
            complete: function () {
                $("#save_etl_task").removeAttr('disabled');
                console.info("complete")
            },
            //报错
            error: function (data) {
                $("#save_etl_task").removeAttr('disabled');
                console.info("error: " + data.responseText);
            }
        });

    });


    $('#update_etl_task').click(function () {

        if ($('#beacon_fire_context').val() == '') {
            layer.msg("烽火台说明不能为空！");
            return;
        }


        var url = location.search; //这一条语句获取了包括问号开始到参数的最后，不包括前面的路径
        var params = url.substr(1);//去掉问号
        var pa = params.split("&");
        var s = new Object();
        for (var i = 0; i < pa.length; i++) {
            s[pa[i].split("=")[0]] = unescape(pa[i].split("=")[1]);
        }

        var editor_str = "&sql_script="+window.editor_sql_script.getValue();
        editor_str = editor_str +"&groovy_script="+window.editor_groovy_script.getValue();

        $.ajax({
            type: 'POST',
            url: server_context+"/beacon_fire_update",
            dataType: 'json',
            async: false,
            data: $("#etl_task_add_form").serialize()+editor_str  + "&" + "id=" + s.id,
            //发送数据前
            beforeSend: function () {
                // 禁用按钮防止重复提交
                $("#update_etl_task").attr({disabled: "disabled"});
            },
            //成功返回
            success: function (data) {
                $("#update_etl_task").removeAttr('disabled');
                if(data.code != '200'){
                    console.error(data.msg);
                    layer.msg("执行失败");
                    return ;
                }
                layer.msg("执行成功");
                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                parent.layer.close(index); //再执行关闭
            },
            //处理完成
            complete: function () {
                console.info("complete")
            },
            //报错
            error: function (data) {
                $("#update_etl_task").removeAttr('disabled');
                layer.error(data.responseText);
                console.info("error: " + data.responseText);
            }
        });

    })

</script>

</body>

</html>
